jsx3.Class.defineClass(
		"tibco.admin.monitormanage.uicomponents.text.serviceAssemblyHeader.ServiceAssemblyHeader", //the full name of the class to create
		jsx3.gui.Block, //  extends
		null, // implements
		function (plugin)
		{
			plugin.prototype.isRunning;
			plugin.prototype.serviceAssemblyId;
			plugin.prototype.serviceAssemblyName;

			plugin.prototype.init = function()
			{
				this.jsxsuper();
			}

			plugin.prototype.getNamespace = function()
			{
				return "tibco.admin";
			}

			plugin.prototype.setUpComponent = function()
			{
				this.isRunning = false;
				var subject = tibco.admin.monitormanage.uicomponents.page.deployment.Deployment.serviceAssemblyInfoCacheSubject;
				tibco.admin.monitormanage.general.PageBusUtils.subscribe(subject, this.handleDisplayScreen, this);
			}

			plugin.prototype.handleDisplayScreen = function(subject, message, headerObj)
			{
				headerObj.loadContent(headerObj);
			}

			plugin.prototype.loadContent = function(headerObj)
			{
				var serviceAssemblyInfoDoc = tibco.admin.Main.getServer().Cache.getDocument(tibco.admin.monitormanage.uicomponents.page.deployment.Deployment.serviceAssemblyInfoCacheId);
				if (serviceAssemblyInfoDoc)
				{
					var record = serviceAssemblyInfoDoc.getFirstChild();
					var MMUtilsNS = tibco.admin.monitormanage.general.MonitorManageUtils;

					headerObj.serviceAssemblyId = record.getAttribute("key");
					headerObj.serviceAssemblyName = record.getAttribute("name");

					var nameField = headerObj.getDescendantOfName("svcAsmblyHeaderName_MM_dply");
					nameField.setText("<span>" + record.getAttribute("name") + "</span>");
					nameField.repaint();

					var statusField1 = headerObj.getDescendantOfName("svcAsmblyHeaderStatusLbl1_MM_dply");
					var statusField2 = headerObj.getDescendantOfName("svcAsmblyHeaderStatusIcon_MM_dply");
					var statusField3 = headerObj.getDescendantOfName("svcAsmblyHeaderStatusLbl2_MM_dply");
					var startStopButton = headerObj.getDescendantOfName("svcAsmblyHeaderStartStopBtn_MM_dply");
					var statusVal = record.getAttribute("status");
					var statusColor;
					var startStopButtonText;
					var imagePath;
					var server = tibco.admin.Main.getServer();
					var statusValidForButtons = false;
					if (statusVal == "Running" || statusVal == "RUNNING")
					{
						statusColor = "#67B54F";
						imagePath = "jsxaddin://user!MatrixAdmin/images/matrix/started_running.gif";
						startStopButtonText = server.getDynamicProperty("@MM@general@headerStopBtnTitle");
						headerObj.isRunning = true;
						statusValidForButtons = true;
					}
					else if(statusVal == "Stopped" || statusVal == "Deployed" || statusVal == "DEPLOYED")
					{
						statusColor = "#B7564F";
						imagePath = "jsxaddin://user!MatrixAdmin/images/matrix/stopped.gif";
						startStopButtonText = server.getDynamicProperty("@MM@general@headerStartBtnTitle");
						headerObj.isRunning = false;
						statusValidForButtons = true;
					}
					var statusText1 = "<span style='margin-left:10px;color:#9898a5;font-weight:bold;'>";
					statusText1 += server.getDynamicProperty("@MM@general@statusLabel");
					statusText1 += "</span>";
					statusField1.setText(statusText1);

					var statusText2;
					statusVal = tibco.admin.monitormanage.general.MonitorManageUtils.handleSAStatusExternalization(statusVal);
					if(statusValidForButtons)
					{
						statusText2 = "&nbsp;&nbsp;<font color=\"" + statusColor + "\">" + statusVal + "</font>";
						statusField2.setSrc(imagePath);
						statusField2.setDisplay(jsx3.gui.Block.DISPLAYBLOCK);
						startStopButton.setDisplay(jsx3.gui.Block.DISPLAYBLOCK);
					}
					else
					{
						// the Service Assembly is not in a state to be stopped or started yet
						statusText2 = "&nbsp;&nbsp;" + statusVal;
						statusField2.setDisplay(jsx3.gui.Block.DISPLAYNONE);
						startStopButton.setDisplay(jsx3.gui.Block.DISPLAYNONE);
					}
					statusField3.setText(statusText2);
					startStopButton.setText(startStopButtonText);
					headerObj.repaint();
				}
			}

			plugin.prototype.startStopServiceAssembly = function()
			{
				try {
					var rulesPath;
					var dataPath;
					var operation;
					if (this.isRunning)
					{
						rulesPath = "tibco/admin/monitormanage/uicomponents/text/serviceAssemblyHeader/rules/stopServiceAssembliesRules.xml";
						dataPath = "tibco/admin/monitormanage/uicomponents/text/serviceAssemblyHeader/data/stopServiceAssembliesData.xml";
						operation = "stopServiceAssemblies";
					}
					else
					{
						rulesPath = "tibco/admin/monitormanage/uicomponents/text/serviceAssemblyHeader/rules/startServiceAssembliesRules.xml";
						dataPath = "tibco/admin/monitormanage/uicomponents/text/serviceAssemblyHeader/data/startServiceAssembliesData.xml";
						operation = "startServiceAssemblies";
					}

					if (tibco.admin.Main.getServer().getEnv("MODE")) {
						// mode is live
						objService = tibco.admin.Main.loadService(this.getNamespace(), rulesPath, operation);
						objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());
						// this setInboundURL should not be required...only here because of a current AMX framework bug
						objService.setInboundURL(dataPath);
					}
					else {
						objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), rulesPath), operation, null, null);
						objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), dataPath));
					}

					objService.serviceAssemblyId = this.serviceAssemblyId;
					objService.serviceAssemblyName = this.serviceAssemblyName;
					jsx3.log("Operation " + operation + " called with inputs " + this.serviceAssemblyId + " and " + this.serviceAssemblyName);

					//subscribe and call
					objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.onstartStopServiceAssembliesSuccess);
					objService.subscribe(jsx3.net.Service.ON_ERROR, this.onstartStopServiceAssembliesError);
					objService.subscribe(jsx3.net.Service.ON_INVALID, this.onstartStopServiceAssembliesInvalid);
					objService.doCall();
				}
				catch(ex) {
					tibco.admin.Main.logException(ex, "Error in tibco.admin.monitormanage.uicomponents.text.serviceAssemblyHeader.ServiceAssemblyHeader.startStopServiceAssembly");
				}
			}

			plugin.prototype.onstartStopServiceAssembliesSuccess = function(objEvent)
			{
				//var responseXML = objEvent.target.getInboundDocument();
				//jsx3.log("Success","The service call was successful.");

				// post a message to call getServiceAssembly again so the status can be refreshed
				var message = { doRefresh:true };
				tibco.admin.monitormanage.general.PageBusUtils.publish(tibco.admin.monitormanage.uicomponents.page.deployment.Deployment.serviceAssemblyPerformRefreshSubject, message);
			};

			plugin.prototype.onstartStopServiceAssembliesError = function(objEvent)
			{
				var myStatus = objEvent.target.getRequest().getStatus();
				jsx3.log("Error", "The service call failed. The HTTP Status code is: " + myStatus);
			};

			plugin.prototype.onstartStopServiceAssembliesInvalid = function(objEvent)
			{
				jsx3.log("Invalid", "The following message node just failed validation:\n\n" + objEvent.message);
			};

		});